home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Scheduling / Cassandra / Source / Today.m < prev    next >
Encoding:
Text File  |  1990-12-09  |  6.2 KB  |  314 lines

  1. //
  2. // Today.m
  3. // Copyright (c) 1989, 1990 by Jiro Nakamura 
  4. // All rights reserved
  5. //
  6. //    by Jiro Nakamura (ac6y@vax5.cit.cornell.edu)
  7. //
  8. // RCS Information
  9. // Revision Number->    $Revision: 2.6 $
  10. // Last Revised->    $Date: 90/12/03 01:56:40 $
  11. //
  12. static char rcsid[] = "$Id: Today.m,v 2.6 90/12/03 01:56:40 jiro Exp Locker: jiro $";
  13.  
  14. #import "Today.h"
  15. #import "Global.h"
  16. #import "Event.h"
  17. #import "cass.h"    // for TODAY_ICON
  18. #import "calendar.h"    // for ascMyDate()
  19.  
  20. #import <appkit/Application.h>     // for NX_MODALRESPTHRESHOLD 
  21. #import <appkit/ScrollView.h>
  22.  
  23. #import <sys/time.h>        
  24. #import <sys/file.h>
  25. #import <libc.h>
  26. #import <strings.h>
  27.  
  28.  
  29. @implementation Today
  30. - open:sender
  31. {
  32.     static alreadyInited = FALSE;
  33.     
  34.     if( !alreadyInited)
  35.         {
  36.         alreadyInited = TRUE;
  37.         [self placeWindow: [global todayFrame]];
  38.         }
  39.  
  40.     [self setWindowIcon:     TODAY_ICON];
  41.     [self openWith:     ""];
  42.     textView = [textScroll docView];
  43.     [textView setDelegate : self];    
  44.     [textView setMonoFont: FALSE];
  45.     [textView setEditable: FALSE];
  46.     miniaturized = FALSE;
  47.     [self setDelegate: self];
  48.     
  49.     return self;
  50. }
  51.  
  52. - save
  53. {
  54.     // This window shouldn't save itself. 
  55.     [self setDocEdited: FALSE];
  56.     [textView becomeFirstResponder];
  57.     return self;
  58. }
  59.  
  60. - close
  61. {
  62.     [global    saveThisWindowPosition:    DEFAULTTODAYFRAME : self];
  63.     [super close];
  64.     miniaturized = FALSE;
  65.     return self;
  66. }
  67.  
  68. - update
  69. {
  70.     NXStream *output; 
  71.     Event *ev;
  72.     int  today, temp;
  73.     static struct tm time;
  74.  
  75.     if( miniaturized || [self isVisible])
  76.         {
  77.         time = *timeNow();
  78.  
  79.         // We want to read the eventFile pointed to by global 
  80.         ev = [Event newAt:[global eventFile]];
  81.  
  82.  
  83.         // We need a temporary memory stream to store the queue.
  84.         if( (output = NXOpenMemory( NULL, 0, NX_READWRITE)) == NULL)
  85.             {
  86.             fprintf(stderr,"%s: Couldn't open memory stream for "
  87.                 "Today queue scrollview. FATAL ERRROR.\n",
  88.                 PROGNAME);
  89.             exit(1) ;
  90.             }
  91.                 
  92.         [ev firstEvent];
  93.         today = [ev mday];
  94.         
  95.         /* If the first event is today, then we just won't print
  96.            anything */
  97.         if( ([ev mday] != time.tm_mday) ||
  98.             ([ev mon]  != time.tm_mon) ||
  99.             ([ev year] != time.tm_year))
  100.                 today = -1;
  101.     
  102.         NXPrintf(output, "{\\rtf0\\ansi{\\fonttbl\\f0\\fmodern "
  103.                 "%s;}\\fs%.0f\n",
  104.                 [global fontName], [global fontSize] * 2);
  105.  
  106.         if( [ev present] == 0 || today == -1)
  107.             NXPrintf(output, 
  108.                 "{\\f0Nothing for today.\\par\\par}\n");
  109.                     
  110.         /* Print all events for today regardless of priority */
  111.         while ( [ev present] != 0 && [ev mday] == today)
  112.             {
  113.             if( [ev priority] > [global highPriority])
  114.                 NXPrintf(output, "{\\f0\\b ");
  115.             else
  116.                 NXPrintf(output, "{\\f0 ");
  117.  
  118.             if( [global militaryTime])
  119.                 NXPrintf(output, "%2d:%02d  %s\\par}\n", 
  120.                     [ev hour], [ev min], 
  121.                     [ev message]);
  122.             else
  123.                 {
  124.                 temp = [ev hour];
  125.                 if( temp > 12 ) temp -= 12;
  126.                 if( temp == 0 )
  127.                     temp = 12;            
  128.                 NXPrintf(output, "%2d:%02d %s  "
  129.                     "%s\\par}\n", 
  130.                     temp,    [ev min], 
  131.                     ([ev hour]>11)?"pm":"am",
  132.                     [ev message]);
  133.                 }
  134.             [ev readEvent : [ev next]]; 
  135.             }   
  136.         
  137.         // Get ready to print everything for tomorrow
  138.         today = [ev mday];
  139.         time.tm_mday ++;
  140.         fixTmStructure( &time );
  141.             
  142.         // If the next event isn't tomorrow, then
  143.         // say that there is nothing tomorrow
  144.         if(     ([ev mday] != time.tm_mday) ||
  145.                 ([ev mon]  != time.tm_mon) ||
  146.               ([ev year] != time.tm_year))
  147.                 today = -1;
  148.                 
  149.         NXPrintf(output, "{\\f0 -------------\\par}\n");
  150.  
  151.         if( [ev present] == 0 || today == -1)
  152.             NXPrintf(output, 
  153.                 "{\\f0 Nothing for tomorrow.}\n");
  154.         else
  155.             NXPrintf(output, 
  156.                 "{\\f0\\b Tomorrow: %s\\par\\par}\n",
  157.                 ascMyDate( &time));
  158.         
  159.         while ( [ev present] != 0 && [ev mday] == today)
  160.             {
  161.             if( [ev priority] > [global highPriority])
  162.                 NXPrintf(output, "{\\f0\\b ");
  163.             else
  164.                 NXPrintf(output, "{\\f0 ");
  165.  
  166.             if( [global militaryTime])
  167.                 NXPrintf(output, "%2d:%02d  "
  168.                     "%s\\par}\n", 
  169.                     [ev hour], [ev min], 
  170.                     [ev message]);
  171.             else
  172.                 {
  173.                 temp = [ev hour];
  174.             
  175.                 if( temp > 12 )
  176.                     temp -= 12;
  177.                 if( temp == 0 )
  178.                     temp = 12;            
  179.             
  180.                 NXPrintf(output, "%2d:%02d %s  "
  181.                         "%s\\par}\n", 
  182.                         temp,
  183.                         [ev min], 
  184.                         ([ev hour]>11)?
  185.                         "pm":"am",
  186.                         [ev message]);
  187.                 }
  188.  
  189.             [ev readEvent : [ev next]]; 
  190.             }    
  191.  
  192.         NXPrintf(output, "}\n");    
  193.         
  194.  
  195.         NXSeek(output, 0, NX_FROMSTART);
  196.         [textView readRichText: output]; 
  197.         NXCloseMemory(output, NX_FREEBUFFER); 
  198.     
  199.         [self setDocEdited: FALSE];
  200.         [textView becomeFirstResponder];
  201.         [textView hideCaret];    // for some reason the Text
  202.         [textView update];    // object sometimes leaves a caret...
  203.         
  204.         [ev free];        
  205.         [self timeUpdate: self];
  206.         }    
  207.     
  208.     return self;
  209. }
  210.  
  211. - timeUpdate:sender
  212. {
  213.     static char     *timeMark;
  214.     static char buf[30];
  215.     static struct tm time;
  216.     extern const char *shortMonths[12];
  217.     extern const char *shortWeekDays[7];
  218.  
  219.     if ( !miniaturized && [self isVisible])
  220.     {
  221.         time = *timeNow();
  222.         if (!showTime)
  223.             {
  224.             if( !miniaturized)
  225.                 sprintf(buf, "%s - Today", ascMyDate(&time));
  226.             else
  227.                 sprintf(buf, "%s %s %2d",
  228.                     shortWeekDays[time.tm_wday],
  229.                     shortMonths[time.tm_mon],
  230.                     time.tm_mday);
  231.             }
  232.         else
  233.         {
  234.             if( [self isVisible])
  235.                 sprintf(buf, "%s - Today", ascMyTime(&time, 
  236.                     showSeconds, militaryTime));
  237.             else
  238.                 {    
  239.                 if( minute == time.tm_min)
  240.                     return self;
  241.                 else
  242.                     minute = time.tm_min;
  243.                         
  244.                 hour = time.tm_hour;
  245.                 if( militaryTime)
  246.                     timeMark = "";
  247.                 else
  248.                     {
  249.                     if (hour > 11)
  250.                         timeMark = "p";
  251.                     else
  252.                         timeMark = "a";
  253.         
  254.                     if (hour > 12)
  255.                         hour -= 12;
  256.                     if (hour == 0)
  257.                         hour = 12;
  258.                     }
  259.                 sprintf(buf, "%d:%02d%s %d/%d", hour, 
  260.                     time.tm_min, timeMark, time.tm_mon+1, 
  261.                     time.tm_mday);
  262.                 }
  263.         }
  264.         [self setTitle:buf];
  265.     }
  266.     return self;
  267. }
  268.  
  269. - defaultsDidChange: sender
  270. {
  271.     #ifdef DEBUG
  272.         fprintf(stderr,"Defaults changed.... Overview compensating\n");
  273.     #endif
  274.     
  275.     militaryTime     = [global militaryTime];
  276.     showSeconds     = [global showSeconds];
  277.     showTime    = [global todayShowTime];
  278.     
  279.     [self update];
  280.     return self;
  281. }
  282.  
  283. - windowDidMiniaturize: sender
  284. {
  285.     #ifdef DEBUG
  286.         fprintf(stderr,"Today did miniaturize\n");
  287.     #endif
  288.  
  289.     miniaturized = TRUE;
  290.     minute = -1;        // Force a redisplay on reopening
  291.     return self;
  292. }
  293.  
  294. - windowDidDeminiaturize: sender
  295. {
  296.     #ifdef DEBUG
  297.         fprintf(stderr,"Today did deminiaturize\n");
  298.     #endif
  299.  
  300.     miniaturized = FALSE;
  301.     minute = -1;        // Force a redisplay
  302.     [self timeUpdate:self];
  303.     return self;
  304. }
  305.  
  306.  
  307. -setGlobal:anObject
  308. {
  309.     global = anObject;
  310.     return self;
  311. }
  312.  
  313. @end
  314.